#include "qFinNallista.h"
#include <QSqlQuery>
#include <QSqlRecord>
#include <QDesktopWidget>


QFinNalLista::QFinNalLista(QWidget *parent)
	: QMyBaseFormWidget(parent)
{
	ui.setupUi(this);
	QDesktopWidget desk;
	QRect deskRect = desk.screenGeometry();
	ui.layoutWidget->setFixedWidth(deskRect.width() - 300);
	ui.layoutWidget->setFixedHeight(deskRect.height()-200);
	ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
	ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.tableView_2->setSelectionMode(QAbstractItemView::SingleSelection);
	ui.tableView_2->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.tableView->setSortingEnabled(true);
	connect(ui.tableView->horizontalHeader(),SIGNAL(sectionClicked(int)), ui.tableView, SLOT(sortByColumn(int)));
// 	ui.tableView_2->setSortingEnabled(true);
// 	connect(ui.tableView_2->horizontalHeader(),SIGNAL(sectionClicked(int)), ui.tableView_2, SLOT(sortByColumn(int)));

	lista("%%%");
	m_selectedText = model->item(0, 0) ? model->item(0, 0)->text():"";
	lista_detail(m_selectedText);
	ui.tableView->setFocus();
}

QFinNalLista::~QFinNalLista()
{

}
void QFinNalLista::pressEscape()
{
    emit closeW();
}
void QFinNalLista::lista(const QString& nameSearch)
{
    setUpdatesEnabled(false);
    if (nameSearch.length() < 0)
    {
        ui.tableView->hide();
        return;
    }
    int r = 0;
    int c = 3;
    int fieldNo[3];
    QString country[3];
    int colWidth[] = { 80, 100, 300};
    QString colName[] = {"id", "dok_id", "datum", "naziv", "adresa", "grad"};
    QString temp = 
        "select nalog_br, datum, opis "
        "from FinansoviNalozi ";
    QSqlQuery query(temp);
    model = new QStandardItemModel(r,c);
    model->setHeaderData( 0, Qt::Horizontal, trUtf8("Налог бр."));
    model->setHeaderData( 1, Qt::Horizontal, trUtf8("Датум"));
    model->setHeaderData( 2, Qt::Horizontal, trUtf8("Опис"));

    ui.tableView->setModel(model);
    header = new QHeaderView(Qt::Horizontal, this);
	header->setClickable(true);
    ui.tableView->setHorizontalHeader(header);
    header->show();
    int row = 0;
    while (query.next()) 
    {
        for (int i = 0; i < c; i++)
        {
            QString temp = query.value(i).toString();
            if (i == 1)
            {
                temp = temp.left(10);
            }
            QStandardItem *item = new QStandardItem(temp);
            item->setEditable(false);
            ui.tableView->setRowHeight(row, 18);
            ui.tableView->setColumnWidth(i, colWidth[i]);
            model->setItem(row, i, item);
        }
        row++;
    } 
		setUpdatesEnabled(true);
		QItemSelectionModel *sm = ui.tableView->selectionModel();
		connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
        this, SLOT(selectionChanged(QModelIndex,QModelIndex)));
	    ui.tableView->show();
}
void QFinNalLista::selectionChanged(QModelIndex modelX, QModelIndex modelY)
{
    int i = modelX.row();
    m_selectedText = model->item(i, 0) ? model->item(i, 0)->text():"";

    lista_detail(m_selectedText);

}
void QFinNalLista::lista_detail(const QString& nameSearch)
{
    QLocale loc;
    if (nameSearch.length() < 0)
    {
        ui.tableView_2->hide();
        return;
    }

    int r = 0;
    int c = 8;
    int colWidth[] = {80, 100, 100, 220, 80, 100, 100, 100};
    QString temp = 
        "select nalog_br, datum_knizenje, datum_dokument, sodrzina, konto, dolzi, pobaruva "
        "from FinansoviNalozi_stavki                                         "
        "where nalog_br=  ";
    temp += m_selectedText;
    temp += "";
    QSqlQuery query(temp);

    model2 = new QStandardItemModel(r,c);
    model2->setHeaderData( 0, Qt::Horizontal, trUtf8("НалогБр.."));
    model2->setHeaderData( 1, Qt::Horizontal, trUtf8("Датум\nКнижење"));
    model2->setHeaderData( 2, Qt::Horizontal, trUtf8("Датум\nДокумент"));
    model2->setHeaderData( 3, Qt::Horizontal, trUtf8("Содржина"));
    model2->setHeaderData( 4, Qt::Horizontal, trUtf8("Конто"));
    model2->setHeaderData( 5, Qt::Horizontal, trUtf8("Должи"));
    model2->setHeaderData( 6, Qt::Horizontal, trUtf8("Побарува"));
	model2->setHeaderData( 7, Qt::Horizontal, trUtf8("Салдо"));

    ui.tableView_2->setModel(model2);
    header2 = new QHeaderView(Qt::Horizontal, this);
	header2->setClickable(true);
    ui.tableView_2->setHorizontalHeader(header2);
    header2->show();
    int row = 0;
	float saldo = 0.f;
	while (query.next()) 
    {
        for (int i = 0; i < c; i++)
        {
			QStandardItem *item = NULL;

			QString temp = "";
			if ( i == 7)
            {
				saldo = saldo + query.value(5).toFloat() - query.value(6).toFloat();
				temp = loc.toString(saldo , 'f', 2);
				item = new QStandardItem(temp);
				item->setTextAlignment(Qt::AlignRight);
            } 
            else if((i == 5)||( i == 6))
            {
				temp = loc.toString(query.value(i).toFloat(), 'f', 2);
				item = new QStandardItem(temp);
				item->setTextAlignment(Qt::AlignRight);
			}
			else
			{
				temp = query.value(i).toString();
				item = new QStandardItem(temp);
				item->setTextAlignment(Qt::AlignLeft);
			}

            item->setEditable(false);
            ui.tableView_2->setRowHeight(row, 18);
            ui.tableView_2->setColumnWidth(i, colWidth[i]);
            model2->setItem(row, i, item);
        }
        row++;
    } 
    ui.tableView_2->show();
}

void QFinNalLista::deleteDok(int idDok)
{
//     QSqlQuery query;
//     query.clear();
//     query.prepare(
//         "delete from magacin where dok_tip = 40 and dok_id = :a"
//         );
//     query.bindValue(":a", idDok);	
//     query.exec();
}
void QFinNalLista::on_pushButton_clicked()
{
    emit procF2();
}
void QFinNalLista::on_pushButton_2_clicked()
{
    emit procF3();
}

QStringList QFinNalLista::geInfo()
{
    QStringList info;
    info.clear();
    info << ""; 
    return info;
}
bool QFinNalLista::isCheckedTovarenList()
{
return false;
}

bool QFinNalLista::isCheckedMalaIspratnica()
{
	return false;
}

void QFinNalLista::setRow(int mrow)
{
	ui.tableView->selectRow(mrow);
	ui.tableView->setFocus();
}
int QFinNalLista::getRow()
{
	int m_row = -1;
	QModelIndexList indexes = ui.tableView->selectionModel()->selection().indexes();
	if (indexes.count())
	{
		m_row = indexes.at(0).row();
	}
	return m_row;
}

void QFinNalLista::pressReturn()
{
	if (ui.tableView->hasFocus())
	{
		emit sIspratnicaToIzlFaktura(m_selectedText, m_selectedText);	
	}
}